<?php
/**
 * Created by PhpStorm.
 * User: wuxin
 */

use \yii\helpers\Url;
use \yii\helpers\Html;
use \backend\service\AdminUserService ;
use yii\helpers\ArrayHelper ;
use common\service\ConstatService ;

$typeId = ConstatService::ADMIN_USER_TYPE_ID_GENERAL ; //普通员工
$typeArr = [ConstatService::ADMIN_USER_TYPE_ID_GENERAL,ConstatService::ADMIN_USER_TYPE_ID_MANAGER];
$users = AdminUserService::getUser($typeArr) ;
$usersArr = ArrayHelper::map($users,"user_id","username") ;
$userId = Yii::$app->admin->identity->user_id ;
$typeId = Yii::$app->admin->identity->type_id ;
?>
<form id="add-feed-form" class="form-horizontal" method="post" enctype="multipart/form-data">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel"><?= empty($id)?"添加":"编辑";?>反馈</h4>
    </div>
    <div style="border-bottom: 1px solid #e5e5e5"></div>
    <div class="modal-body">
        <div class="box-body">
            <div class="form-group">
                <label class="col-sm-4 control-label">问题描述：</label>
                <div class="input-group col-sm-6  required" style="padding-right: 15px;padding-left: 15px;">
                    <textarea class="form-control" id="FeedForm-desc" name="FeedForm[desc]" rows="5"></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label">公司名称：</label>
                <div class="input-group col-sm-6  required" style="padding-right: 15px;padding-left: 15px;">
                    <input class="form-control" type="text" id="FeedForm-company_name" name="FeedForm[company_name]">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-4 control-label">联系人：</label>
                <div class="input-group col-sm-6  required" style="padding-right: 15px;padding-left: 15px;">
                    <input class="form-control" type="text" id="FeedForm-contact_name" name="FeedForm[contact_name]">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-4 control-label">联系方式：</label>
                <div class="input-group col-sm-6  required" style="padding-right: 15px;padding-left: 15px;">
                    <input class="form-control" type="text" id="FeedForm-contact" name="FeedForm[contact]">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-4 control-label">地址：</label>
                <div class="input-group col-sm-6  required" style="padding-right: 15px;padding-left: 15px;">
                    <input class="form-control" type="text" id="FeedForm-address" name="FeedForm[address]">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-4 control-label">问题图片一：</label>
                <div class="input-group col-sm-6  required" style="padding-right: 15px;padding-left: 15px;">
                    <input class="form-control" type="file" id="FeedForm-pic_url1" name="FeedForm[pic_url1]" accept="image/*">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-4 control-label">问题图片二：</label>
                <div class="input-group col-sm-6  required" style="padding-right: 15px;padding-left: 15px;">
                    <input class="form-control" type="file" id="FeedForm-pic_url2" name="FeedForm[pic_url2]" accept="image/*">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-4 control-label">问题图片三：</label>
                <div class="input-group col-sm-6  required" style="padding-right: 15px;padding-left: 15px;">
                    <input class="form-control" type="file" id="FeedForm-pic_url3" name="FeedForm[pic_url3]" accept="image/*">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-4 control-label">视频：</label>
                <div class="input-group col-sm-6  required" style="padding-right: 15px;padding-left: 15px;">
                    <input class="form-control" type="file" id="FeedForm-video_url" name="FeedForm[video_url]" accept="video/*">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-4 control-label">备注：</label>
                <div class="input-group col-sm-6  required" style="padding-right: 15px;padding-left: 15px;">
                    <textarea class="form-control" id="FeedForm-remark" name="FeedForm[remark]" rows="5"></textarea>
                </div>
            </div>

            <?php if($typeId==ConstatService::ADMIN_USER_TYPE_ID_MANAGER ||$typeId == ConstatService::ADMIN_USER_TYPE_ID_SUPER_ADMIN):?>
            <div class="form-group">
                <label class="col-sm-4 control-label" >处理人：</label>
                <div class="input-group col-sm-6  required" style="padding-right: 15px;padding-left: 15px;">
                    <select name="FeedForm[deal_user_id]" class="form-control">
                        <option value="0">请选择</option>
                        <?php foreach ($usersArr as $key=>$item):?>
                            <option value="<?= $key ?>"><?= $item ?></option>
                        <?php endforeach ?>
                    </select>
                </div>
            </div>
            <?php else : ?>
                <input type="hidden" name="FeedForm[deal_user_id]" value="<?=$userId?>">
            <?php endif;?>

        </div>
    </div>
    <div class="modal-footer">
        <?= Html::hiddenInput('_csrf-backend',\Yii::$app->getRequest()->csrfToken) ?>
        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
        <button type="button" class="btn btn-primary" id="add-contral-ajax-submit">保存</button>
    </div>
</form>
<script>
    $('#add-contral-ajax-submit').click(function () {
        var data = new FormData(document.getElementById("add-feed-form"));//表单id
        console.log(data);
        var button = $(this) ;
        var buttonText = $(this).text();
        var url = '<?= Url::toRoute('add-feed') ?>' ;
        $.ajax({
            type: "POST",
            url: url,
            data: data,
            dataType: "json",
            cache: false,//上传文件无需缓存
            processData: false,//用于对data参数进行序列化处理 这里必须false
            contentType: false, //必须
            beforeSend: function () {
                showMask();
                button.text(buttonText + '中');
                button.attr('disabled', 'disabled');
            },
            success: function(data){
                button.text(buttonText);
                button.removeAttr('disabled');
                if (typeof(data.code) != "undefined" && data.code == 200) {
                    bootboxAlert(data.msg);
                    if (data.url) {
                        setTimeout(function(){window.location.href = data.url;},1000);
                    }
                    if(typeof callback === "function") {
                        callback(data.data);
                    }
                } else if (typeof(data.code) != "undefined") {
                    bootboxAlert(data.msg);
                }
            },
            error: function (data) {
                if (data.status !=  302) {
                    bootboxAlert(data.responseText);
                }
                button.text(buttonText);
                button.removeAttr('disabled');
            },
            complete:function(){
                hideMask() ;
            }
        });
    });
</script>